nice_things/text/substitute_characters.sh
substitute_characters
Since 0.3.0 · Source
import "{ substitute_characters }" from nice_things/text/substitute_characters.sh
Synopsissubstitute_characters <pattern> <replacement> [<text>]
Configuration
–
Description
Substitute every instance of the <pattern> characters in <text> with <replacement> string, and print the result. If the <text> parameter is omitted, text will be read from stdin.
This function uses only shell builtins and has no external dependencies (e.g. on sed). This is slower than using sed on large inputs, but can be faster on many invocations with small inputs, since it avoids forking a new process.
Options
–
Operands
<pattern>: A string with one or more characters to be searched.<replacement>: A string to replace any matched characters.<text>: The text to operate on. If omitted, stdin will be used.
Stdin
The text will be read from stdin if the <text> parameter is omitted.
Stdout
The result will be printed to stdout.
Stderr
–
Exit status0: Successful completion.
Abort
–
Usage examples
lf='
'
# Escape all backslash characters '\\', read from stdin
assign escaped_text substitute_characters '\' '\\' </path/to/file
# Encode all line-feed characters as '\n'
assign escaped_text substitute_characters "$lf" '\n' "$escaped_text"